package com.zxy.ziems.server.controller;

import com.zxy.btp.common.response.JsonResponse;
import com.zxy.btp.common.response.PageResult;
import com.zxy.ziems.server.dto.OrgDto;
import com.zxy.ziems.server.dto.OrgQueryDto;
import com.zxy.ziems.server.dto.OrgTreeNode;
import com.zxy.ziems.server.foms.dto.DeptInfoTree;
import com.zxy.ziems.server.foms.service.FomsRpcService;
import com.zxy.ziems.server.foms.service.TenantUtil;
import com.zxy.ziems.server.service.OrgService;
import com.zxy.ziems.server.vo.OrgVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


/**
 * 组织管理
 * @author bhg
 * @date 2023/3/9
 */
@RestController
@RequestMapping("/api/org")
public class OrgController {
    @Autowired
    private OrgService orgService;
    @Autowired
    private FomsRpcService fomsRpcService;

    /**
     * 分页查询
     * @param orgQueryDto
     * @return
     */
    @PostMapping("/page/list")
    public JsonResponse<PageResult<OrgVo>> pageList(@RequestBody OrgQueryDto orgQueryDto){
        PageResult<OrgVo> pageResult = orgService.pageList(orgQueryDto);
        return JsonResponse.SUCCESS(pageResult);
    }

    /**
     * 新增
     * @param orgDto
     * @return
     */
    @PostMapping("/addOrUpdate")
    public JsonResponse addOrUpdate(@RequestBody @Validated OrgDto orgDto){
        orgService.addOrUpdate(orgDto);
        return JsonResponse.SUCCESS();
    }


    /**
     * 删除
     * @param orgId
     * @return
     */
    @GetMapping("/delete")
    public JsonResponse delete(String orgId){
        orgService.deleteById(orgId);
        return JsonResponse.SUCCESS();
    }

    /**
     * 详情
     * @param orgId
     * @return
     */
    @GetMapping("/detail")
    public JsonResponse detail(String orgId){
        OrgVo orgVo = orgService.detail(orgId);
        return JsonResponse.SUCCESS(orgVo);
    }

    /**
     * 查询子节点
     * @param parentOrgId 父节点的orgId
     * @return
     */
    @GetMapping("/children")
    public JsonResponse<List<OrgVo>> children(String parentOrgId){
        List<OrgVo> orgList = orgService.getChildren(parentOrgId);
        return JsonResponse.SUCCESS(orgList);
    }

    /**
     * 组织树
     * @return
     */
    @GetMapping("/tree/old")
    public JsonResponse<List<OrgTreeNode>> tree(){
        List<OrgTreeNode> orgTreeNode = orgService.tree();
        return JsonResponse.SUCCESS(orgTreeNode);
    }

    /**
     * 组织树(zifoms)
     * @return
     */
    @GetMapping("/tree")
    public JsonResponse<List<OrgTreeNode>> treeFoms(){
        List<DeptInfoTree> deptInfoTrees = fomsRpcService.deptTree(null, TenantUtil.getTenantId(), null);

        return JsonResponse.SUCCESS(getChildren(deptInfoTrees));
    }

    private List<OrgTreeNode> getChildren(List<DeptInfoTree> deptInfoTrees) {
        List<OrgTreeNode> treeNodes = new ArrayList<>();
        deptInfoTrees.forEach(m -> {
            OrgTreeNode orgTreeNode = new OrgTreeNode(m.getId(), m.getName(), m.getOrgCode(), m.getParentId());
            treeNodes.add(orgTreeNode);
            if (!CollectionUtils.isEmpty(m.getChildList())) {
                orgTreeNode.setChildren(getChildren(m.getChildList()));
            }
        });
        return treeNodes;
    }


}
